#!/bin/bash

# 색상 설정 (터미널 가독성용)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

echo -e "${GREEN}🔍 커밋 전 자동 검사를 시작합니다...${NC}"

# 1. cpplint (스타일 체크)
echo "1️⃣  Running cpplint..."
cpplint --filter=-legal/copyright,-build/include_subdir,-whitespace --linelength=120 include/*h lib/*cpp src/*cpp
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ cpplint 실패${NC}"
    exit 1
fi

# 2. cppcheck (정적 분석)
echo "2️⃣  Running cppcheck..."
cppcheck --quiet --enable=warning --error-exitcode=1 --suppress=normalCheckLevelMaxBranches --suppress=checkersReport include/ src/
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ cppcheck 실패${NC}"
    exit 1
fi

# 3. 빌드 확인 (CMake)
echo "3️⃣  Building project..."
cmake -S . -B build > /dev/null
cmake --build build
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ 빌드 실패${NC}"
    exit 1
fi

# 4. gtest 실행 (CTest)
echo "4️⃣  Running unit tests..."
cd build && ctest --output-on-failure
if [ $? -ne 0 ]; then
    echo -e "${RED}❌ 테스트 실패: 모든 유닛 테스트를 통과해야 합니다.${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 모든 검사 통과! 커밋을 진행합니다.${NC}"
exit 0